package xaj.scc.ast;

import java.util.List;

import polyglot.ast.Node;
import polyglot.ext.jl.ast.Node_c;
import polyglot.util.CodeWriter;
import polyglot.util.Position;
import polyglot.visit.NodeVisitor;
import polyglot.visit.PrettyPrinter;

public class NamedFactor_c extends Node_c implements NamedFactor {

	private String name;
	private Factor factor;
	
	public NamedFactor_c(Position pos, String name, Factor factor) {
		super(pos);
		this.name = name;
		this.factor = factor;
	}

	public Factor getFactor() {
		return factor;
	}

	public String getName() {
		return name;
	}

	public NamedFactor setFactor(Factor factor) {
		NamedFactor_c n = (NamedFactor_c) copy();
		n.factor = factor;
		return n;
	}

	public NamedFactor setName(String name) {
		NamedFactor_c n = (NamedFactor_c) copy();
		n.name = name;
		return n;
	}

	protected NamedFactor_c reconstruct(Factor factor) {
		if(this.factor != factor) {
			NamedFactor_c n = (NamedFactor_c) copy();
			n.factor = factor;
			return n;
		}
		return this;
	}
	
	public Node visitChildren(NodeVisitor v) {
		Factor factor = (Factor) this.factor.visit(v);
		return reconstruct(factor);
	}
	
	public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
		w.write(name + " = ");
		factor.prettyPrint(w, tr);
	}

	@Override
	public void accept(Visitor v) {
		v.visitNamedFactor(this);
	}
}
